﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VSClientTest.VSServiceReference;
using System.ServiceModel;
using System.Xml;

namespace VSClientTest
{
    public class Program
    {
        static void Main(string[] args)
        {
            var binding = new WSHttpBinding();
            var readerQuotas = new XmlDictionaryReaderQuotas();

            Console.WriteLine("Connecting to service");

            readerQuotas.MaxStringContentLength = Int32.MaxValue;
            readerQuotas.MaxArrayLength = Int32.MaxValue;
            readerQuotas.MaxBytesPerRead = Int32.MaxValue;
            readerQuotas.MaxDepth = Int32.MaxValue;
            readerQuotas.MaxNameTableCharCount = Int32.MaxValue;

            binding.SendTimeout = TimeSpan.FromMinutes(2);
            binding.OpenTimeout = TimeSpan.FromMinutes(2);
            binding.CloseTimeout = TimeSpan.FromMinutes(1);
            binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            binding.AllowCookies = false;
            binding.BypassProxyOnLocal = false;
            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            binding.MessageEncoding = WSMessageEncoding.Text;
            binding.TextEncoding = System.Text.Encoding.UTF8;
            binding.UseDefaultWebProxy = true;
            binding.MaxBufferPoolSize = int.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.ReaderQuotas = readerQuotas;

            var client = new VSServiceClient(binding, new EndpointAddress("http://localhost:49586/VSService.svc"));
            var userID = Guid.NewGuid();

            client.OpenSession(userID);
            client.OpenVisualStudio(userID);

            Console.WriteLine("Opening solution");

            var solution = client.OpenVSSolution(userID, @"E:Mind Chemistry\SolutionFramework\SolutionFramework\SolutionFramework.sln");

            Console.WriteLine("Opened solution: " + solution.Name);

            foreach (var project in client.GetProjects(userID, solution))
            {
                Console.WriteLine(new string(' ', 4) + "Project: " + project.Name);

                foreach (var model in client.GetModels(userID, project))
                {
                    int x = 0;

                    Console.WriteLine(new string(' ', 8) + "Model: " + model.Name);

                    foreach (var fileName in model.FileNames)
                    {
                        Console.WriteLine(new string(' ', 12) + "File: " + fileName);

                        var bytes = client.GetFileContents(userID, model, x);
                        var content = Encoding.ASCII.GetString(bytes);

                        Console.WriteLine("File Contents ---------------------------\r\n");
                        Console.WriteLine(content);
                        Console.WriteLine("End of Contents -------------------------\r\n");

                        x++;
                    }

                    Console.WriteLine(new string(' ', 8) + "End Model");
                }

                Console.WriteLine(new string(' ', 4) + "End Project");
            }


            Console.WriteLine("End solution");

            Console.ReadLine();

            client.CloseSession(userID);
        }
    }
}
